package com.huonan.jb.tools.fileuse.export;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.beust.jcommander.internal.Lists;
import com.huonan.jb.tools.fileuse.compare.FileHandler;
import com.huonan.utils.ExecutorUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.*;
import java.util.concurrent.ExecutorService;

@Slf4j
public class AppExtractData {
    public static void main(String[] args) {
        for (String arg : args) {
            log.info("arg: {}", arg);
        }
        /*输入文件路径*/
        String sourcePath = args[0];
        /*输出文件路径*/
        String outDir = args[1];
        int depth = Integer.parseInt(args[2]);
        handler(sourcePath, outDir, depth);
    }

    private static void handler(String sourcePath, String outDir, int depth) {

        List<Object> list1 = Lists.newArrayList("[\"YSL/圣罗兰\"]", "[\"SOAP&GLORY\"]", "[\"OLAY PCC/OLAY身体护理\"]",
                "[\"Nivea/妮维雅\"]", "[\"L" +
                "'OREAL" +
                " HAIR/巴黎欧莱雅美发\"]", "[\"honor/荣耀\"]", "[\"Hawkins&Brimble\"]", "[\"Estee Lauder/雅诗兰黛\"]");
        for (Object s : list1) {
            System.out.println("===");
            System.out.println(s);
        }
        ExecutorService threadPool = ExecutorUtil.getExecutorService(3);
        List<File> allFiles = FileHandler.traverseAllFiles(sourcePath, depth);
        log.info("总文件数量:{}", allFiles.size());
        Set<String> unicSet = new HashSet<>();
        List<String> allList = new ArrayList<>();
        TimeInterval timer = DateUtil.timer();

        allFiles.forEach(file -> {
            log.info("正在处理文件:{}", file);
            List<String> lines = FileUtil.readUtf8Lines(file);
            log.info("数据量:{}", lines.size());
            List<String> needData = FileHandler.extractJsonData(lines);
            for (String needDatum : needData) {
                JSONObject entries = JSONUtil.parseObj(needDatum);
                String cooperateBinds = entries.get("kw_cooperateBinds", String.class);
                if (list1.contains(cooperateBinds)) {
                    allList.add(needDatum);
                } else {
                    unicSet.add(needDatum);
                }
            }
            allList.addAll(unicSet);
        });
        log.info("去重数据量:{}", allList.size());
        StringJoiner stringJoiner = new StringJoiner(FileUtil.getLineSeparator());
        for (String line : allList) {
            stringJoiner.add(line);
        }
        FileUtil.writeUtf8String(stringJoiner.toString(), outDir);
        long takeTime = timer.intervalSecond();
        log.info("take time:{} s", takeTime);
    }
}
